package com.ruoyi.project.module.wtc.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YhAssist;
import com.ruoyi.project.domain.YhInstructor;
import com.ruoyi.project.service.IYhAssistService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;

import com.ruoyi.common.core.exception.ServiceException;

/**
 * 领用-协助信息Controller
 *
 * @author ruoyi
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/wtc/assist")
public class WtcYhAssistController extends BaseController {
    @Autowired
    private IYhAssistService yhAssistService;

    /**
     * 查询领用-协助信息列表
     */
    //@RequiresPermissions("project:assist:list")
    @GetMapping("/list")
    public TableDataInfo list(YhAssist yhAssist) {
        //startPage();
        yhAssist.setReceiverId(getUsername());
        List<YhAssist> list = yhAssistService.selectYhAssistList(yhAssist);
        return getDataTable(list);
    }

    /**
     * 导出领用-协助信息列表
     */
    //@RequiresPermissions("project:assist:export")
    @Log(title = "领用-协助信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhAssist yhAssist) {
        List<YhAssist> list = yhAssistService.selectYhAssistList(yhAssist);
        ExcelUtil<YhAssist> util = new ExcelUtil<YhAssist>(YhAssist.class);
        util.exportExcel(response, list, "领用-协助信息数据");
    }

    /**
     * 获取领用-协助信息详细信息
     */
    //@RequiresPermissions("project:assist:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(yhAssistService.selectYhAssistById(id));
    }

    /**
     * 新增领用-协助信息
     */
    //@RequiresPermissions("project:assist:add")
    @Log(title = "领用-协助信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhAssist yhAssist) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        yhAssist.setReceiverId(sysUser.getUserName());
        yhAssist.setReceiverName(sysUser.getNickName());
        String userId = getUsername();
        yhAssist.setReceiverId(userId);
        //YhAssist ya = yhAssistService.selectYhAssistByRecId(userId);
        //
        //if (ya == null) {
        //    return toAjax(yhAssistService.insertYhAssist(yhAssist));
        //} else {
        //    yhAssist.setId(ya.getId());
        //    return toAjax(yhAssistService.updateYhAssist(yhAssist));
        //}
        YhAssist yA = new YhAssist();
        yA.setReceiverId(userId);
        yA.setNote1(yhAssist.getNote1());
        List<YhAssist> yhAssistList = yhAssistService.selectYhAssistList(yA);

        if(yhAssistList.size()==0){
            return toAjax(yhAssistService.insertYhAssist(yhAssist));
        }else{
            return toAjax(yhAssistService.updateYhAssist(yhAssist));
        }
    }

    /**
     * 修改领用-协助信息
     */
    //@RequiresPermissions("project:assist:edit")
    @Log(title = "领用-协助信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhAssist yhAssist) {
        return toAjax(yhAssistService.updateYhAssist(yhAssist));
    }

    /**
     * 删除领用-协助信息
     */
    //@RequiresPermissions("project:assist:remove")
    @Log(title = "领用-协助信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhAssistService.deleteYhAssistByIds(ids));
    }


    @GetMapping("/list/a/{id}")
    public YhAssist searchA(@PathVariable("id") String id) {
        String userId = getUsername();
        System.out.println(userId);
        YhAssist yhAssist = new YhAssist();
        yhAssist.setReceiverId(userId);
        yhAssist.setNote1(id);
        //YhAssist ya = yhAssistService.selectYhAssistByRecId(userId);
        List<YhAssist> yhAssistList = yhAssistService.selectYhAssistList(yhAssist);
        //System.out.println(ya);
        //if (ya == null) {
        //    return new YhAssist();
        //} else {
        //    return ya;
        //}
        if(yhAssistList.size()==0){
            return new YhAssist();
        }else{
            return yhAssistList.get(0);
        }
    }

    @GetMapping("/list/{userId}")
    public String searchUser(@PathVariable("userId") String userId) {
        //System.out.println(userId);
        return WtcWeChatAssist.sendWeChatMassage(userId);
    }

    @GetMapping("/list/uId")
    public void checkUser(String name) {
        System.out.println(name);
        if(name==null){
            throw new ServiceException("该用户不存在");
        }
//        else{
//            throw new ServiceException("用户存在");
//        }
    }

    @GetMapping("/role")
    public int assistRole(YhAssist yhAssist)
    {
//        startPage();
        LoginUser loginUser = getLoginUser();
//        SysUser sysUser = loginUser.getSysUser();
        String userID= getUsername();
        yhAssist.setReceiverId(userID);
        List<YhAssist> list = yhAssistService.selectYhAssistList(yhAssist);
        if(list.size()==0){
            return 0;
        }else {
            return 1;
        }

    }
}
